<?php
class LoginForm extends CFormModel{
    public $username;
    public $password;
    private $_identity;
    public function rules() {
        parent::rules();
        return array(
            array('username,password','required'),
            array('password','authenticate'),
        );
    }
    public function authenticate($attribute,$params){
        $this->_identity = new UserIdentity($this->username,$this->password);
        $this->_identity->authenticate();
        switch ($this->_identity->errorCode) {
            case UserIdentity::ERROR_USERNAME_INVALID:
                $this->addError('username','ユーザIDが存在していません、ご確認ください。');
                break;
            case UserIdentity::ERROR_PASSWORD_INVALID:
                $this->addError('password','入力したパスワードが一致していません、ご確認ください。');
                break;
            case UserIdentity::ERROR_NONE:
                return true;
                break;
        }
    }
    public function attributeLabels() {
        parent::attributeLabels();
        return array(
            'username'=>'ログインネーム:',
            'password'=>'パスワード:'
            
        );
    }
    public function login(){
        if($this->_identity->errorCode==UserIdentity::ERROR_NONE){
            Yii::app()->user->login($this->_identity);
            return true;
        }
        else{
            return false;
        }
    }
    
}
